package top.bluesword.laboratory.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import top.bluesword.laboratory.bean.DataFragment;
import top.bluesword.laboratory.bean.DataModel;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

/**
 * 二维列表
 * @author 李林峰
 */
@Slf4j
@Controller
@RequestMapping("two-dimensional-list")
public class TwoDimensionalListController {

    private final Random random = new Random();

    @GetMapping("export-by-template")
    public void exportTwoDimensionalList(HttpServletResponse response) {
        try (
            InputStream inputStream = Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("templates/二维列表.xlsx"));
            ServletOutputStream outputStream = response.getOutputStream()
        ) {
            TemplateExportParams params = new TemplateExportParams(inputStream);
            response.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
            response.setHeader("content-disposition", "attachment;filename=" + "test.xlsx");
            Map<String, Object> dataMap = new HashMap<>(1);
            dataMap.put("list", twoDimensionalList());
            dataMap.put("list2", twoDimensionalList());
            ExcelExportUtil.exportExcel(params, dataMap).write(outputStream);
        } catch (IOException e) {
            log.error("导出二维列表", e);
        }
    }

    private List<DataModel> twoDimensionalList() {
        List<DataModel> list = new ArrayList<>();
        list.add(randomDataModel());
        list.add(randomDataModel());
        list.add(randomDataModel());
        return list;
    }

    private DataModel randomDataModel() {
        DataModel dataModel = new DataModel();
        dataModel.setName("dataModelName");
        dataModel.setKey(String.valueOf(random.nextInt()));
        dataModel.setFragments(randomFragments());
        return dataModel;
    }

    private List<DataFragment> randomFragments() {
        List<DataFragment> list = new ArrayList<>();
        list.add(randomDataFragment());
        list.add(randomDataFragment());
        list.add(randomDataFragment());
        list.add(randomDataFragment());
        list.add(randomDataFragment());
        list.add(randomDataFragment());
        return list;
    }

    private DataFragment randomDataFragment() {
        DataFragment dataFragment = new DataFragment();
        long l = random.nextLong();
        dataFragment.setId(l);
        dataFragment.setTitle("标题"+l);
        dataFragment.setContent("内容"+l);
        return dataFragment;
    }

}
